package com.motu.vertx.module.utility.base;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.jackson.DatabindCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 服务器配置（适用于vertx4.5以上版本)
 */
public class ServerConfig {

    private static final Logger logger = LoggerFactory.getLogger("ServerConfig");

    private static boolean isLocalServer;//是否为本地调试的服务器
    private static String localName;//本地eventBus的别名
    private static boolean isBackups;// 是否是备用进程
    private static int debugType;// 0-正式服 1-测试服 2-评审服
    private static int serverType;//"game"-游戏服 "timedtask"-定时器  "robot"-机器人 "web"-管理后台

    private static boolean isGameInit;//是否是gameinit进程
    private static boolean isTimedTaskWorker;//是否是定时服worker进程
    private static boolean isWechatGame;//微信小游戏的协议用json格式
    private static String gateName = "";//网关进程名称
    private static String loginName = "";//登录进程名称
    private static JsonObject appConfig;// 进程配置信息

    /**
     * 初始化服务器配置
     * @param config 配置信息
     */
    public static void init(JsonObject config){
        if (config.containsKey("local-name")) {
            isLocalServer = true;
            localName = config.getString("local-name");
        }
        if (config.containsKey("backups")) {
            isBackups = config.getBoolean("backups");
        }
        if (config.containsKey("debug-type")) {
            debugType = config.getInteger("debug-type");
        }
        if (config.containsKey("wechat-game")) {
            isWechatGame = config.getBoolean("wechat-game");
        }

        if (config.containsKey("game-config")) {
            appConfig = config.getJsonObject("game-config");
            serverType = ServerTypeEnum.GAME.value;
            if (appConfig.containsKey("game-init-verticle")) {
                isGameInit = appConfig.getBoolean("game-init-verticle");
            }
        } else if (config.containsKey("timedtask-config")) {
            appConfig = config.getJsonObject("timedtask-config");
            serverType = ServerTypeEnum.TIMED_TASK.value;
			if (appConfig.containsKey("worker")) {
				isTimedTaskWorker = appConfig.getBoolean("worker");
			}
        } else if (config.containsKey("robot-config")) {
            appConfig = config.getJsonObject("robot-config");
            serverType = ServerTypeEnum.ROBOT.value;
        } else if (config.containsKey("web-config")) {
            appConfig = config.getJsonObject("web-config");
            serverType = ServerTypeEnum.WEB.value;
        } else if (config.containsKey("reload-config")) {
            appConfig = config.getJsonObject("reload-config");
            serverType = ServerTypeEnum.RELOAD.value;
        } else if (config.containsKey("gate-config")) {
            appConfig = config.getJsonObject("gate-config");
            serverType = ServerTypeEnum.GATE.value;
            gateName = appConfig.getString("gate-name");
        } else if (config.containsKey("login-config")) {
            appConfig = config.getJsonObject("login-config");
            serverType = ServerTypeEnum.LOGIN.value;
            loginName = appConfig.getString("login-name");
        }

        appConfig = Tool.loadCommonConfig(appConfig, isLocalServer);// 加载通用配置信息

        // json转实体类如果json有实体类没有的字段不会报错
        DatabindCodec.mapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		logger.info("ServerConfig init debugType:{} 【0-正式服 1-测试服 2-评审服】 serverType:{}", debugType, serverType);

    }

    public static JsonObject getAppConfig() {
        return appConfig;
    }

    public static int getServerType() {
        return serverType;
    }

    public static String getLocalName() {
        return localName;
    }

    public static boolean isLocalServer() {
        return isLocalServer;
    }

    public static String getLoginName() {
        return loginName;
    }

    public static String getGateName() {
        return gateName;
    }

    public static boolean isWechatGame() {
        return isWechatGame;
    }

    public static boolean isGameInit() {
        return isGameInit;
    }

	public static boolean isTimedTaskWorker() {
		return isTimedTaskWorker;
	}

    public static boolean isBackups() {
        return isBackups;
    }

    public static int getDebugType() {
        return debugType;
    }

    /**
     * 是否是正式服
     */
    public static boolean isOnlineServer() {
        return debugType == 0;
    }

    /**
     * 是否是测试服
     */
    public static boolean isDebugServer() {
        return debugType == 1;
    }

    /**
     * 是否是评审服
     */
    public static boolean isReviewServer() {
        return debugType == 2;
    }

	/**
	 * 是否是登录服
	 */
	public static boolean isLoginServer() {
		return serverType == ServerTypeEnum.LOGIN.value;
	}

	/**
	 * 是否是网关服
	 */
	public static boolean isGateServer() {
		return serverType == ServerTypeEnum.GATE.value;
	}

	/**
	 * 是否是游戏服
	 */
	public static boolean isGameServer() {
		return serverType == ServerTypeEnum.GAME.value;
	}

	/**
	 * 是否是定时服
	 */
	public static boolean isTimedTaskServer() {
		return serverType == ServerTypeEnum.TIMED_TASK.value;
	}

	/**
	 * 是否是web服
	 */
	public static boolean isWebServer() {
		return serverType == ServerTypeEnum.WEB.value;
	}

}
